package mo.offer_special;

import mo.common.TreeNode;

import java.util.LinkedList;

public class L043 {

    private TreeNode root;
    private LinkedList<TreeNode> queue;

    public L043(TreeNode root) {
        this.root = root;
        queue = new LinkedList<>();
    }

    public int insert(int v) {
        if (queue.isEmpty()) {
            queue.add(root);
        }
        TreeNode cur = new TreeNode(v);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node.left == null) {
                node.left = cur;
                queue.addFirst(node);
            } else if (node.right == null) {
                node.right = cur;
                queue.addFirst(node);
            } else {
                queue.add(node.left);
                queue.add(node.right);
                continue;
            }
            return node.val;
        }
        return -1;
    }

    public TreeNode get_root() {
        return root;
    }

}
